<?php
/**     
  *
  * Copyright (c) 2009, Persistent Systems Limited
  *
  * Redistribution and use, with or without modification, are permitted 
  *  provided that the following  conditions are met:
  *   - Redistributions of source code must retain the above copyright notice, 
  *     this list of conditions and the following disclaimer.
  *   - Neither the name of Persistent Systems Limited nor the names of its contributors 
  *     may be used to endorse or promote products derived from this software 
  *     without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
  
/**
  *
  * This file contains the definition of HttpRequest Class
  */
  
/**
  * The HttpRequest Class.
  */
  
class HttpRequest
{
    /**
     * variable holding the cURL handle
     */
    protected $_curlHandle;
    /**
     * The url used for Http request
     */
    protected $_url;
    
    /**
     *
     * @param <string> $httpMethod
     * @param <string> $url
     * @param <Credentail> $credential
     * @param <array> $headers
     * @param <string> $postBody
     * @return No return Value
     * Construct a HttpRequest object
     */
    public function HttpRequest($httpMethod, $url, $credential, $proxy, $headers = array(), $postBody = null)
    {
        $this->_url = $url;
        $this->_curlHandle = curl_init();
        curl_setopt($this->_curlHandle, CURLOPT_URL, $url);
        curl_setopt($this->_curlHandle, CURL_HTTP_VERSION_1_1, true);
        curl_setopt($this->_curlHandle, CURLOPT_HEADER, true);
        curl_setopt($this->_curlHandle, CURLOPT_USERAGENT, "Microsoft ADO.NET Data Services"); 
        if (isset($credential))
        {	
            $userNamePwd = $credential->getUserName() . ":" . $credential->getPassword();
            curl_setopt($this->_curlHandle, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
            curl_setopt($this->_curlHandle, CURLOPT_USERPWD, $userNamePwd);	
        } 
        if (isset($proxy))
        {
            $proxyip_port = $proxy->getProxyAddress() . ":" . $proxy->getProxyPort();
            curl_setopt($this->_curlHandle, CURLOPT_PROXY, $proxyip_port); 
        }   
        curl_setopt($this->_curlHandle, CURLOPT_RETURNTRANSFER,  true);
        curl_setopt($this->_curlHandle, CURLOPT_FOLLOWLOCATION,  true);
        switch ($httpMethod) {
            case 'GET':
                curl_setopt($this->_curlHandle, CURLOPT_HTTPGET, true);
                break;
            case 'POST':
                curl_setopt($this->_curlHandle, CURLOPT_POST, true);
                break;
            default: /*To handle MERGE, DELETE, HEAD etc..*/
                curl_setopt($this->_curlHandle, CURLOPT_CUSTOMREQUEST,  $httpMethod);
                break;
        }        
        if (!is_null($postBody))
        {
            curl_setopt($this->_curlHandle, CURLOPT_POSTFIELDS, $postBody);
            //Here we should to set 'Content-Length' Header which is equal to
            //the length of POST body. But setting this header explicitly 
            //cause server to return 'HTTP Error 400. The request verb is invalid'
            //if we pass credential. Might be in cURL credential may be passed along with
            //POST body, so the length we specifed not be correct. So do not specifiy this
            //header. cURL will set this field based on correct post size
        }
        $curlHeaders = array();
        foreach ($headers as $key => $value)
        {
            $curlHeaders[] = $key.': '.$value;
        }       
        curl_setopt($this->_curlHandle, CURLOPT_HTTPHEADER, $curlHeaders);  
    }
    
    /**
     * @return<string> Raw Http response
     * Perform a HTTP request and return the response as raw string
     */
    public function GetResponse()
    {
        $httpResponse = curl_exec($this->_curlHandle);
        if ($httpResponse)
        {
            curl_close($this->_curlHandle);
            return $httpResponse;
        }
        else
        {
            throw new Exception('Error occured during request for ' . $this->_url . ': ' . curl_error($this->_curlHandle));
        }
    }
}
?>